/**
 * 封装log4js - 日志存储
 * @author sky
 * @email fengyexjtu@126.com
 * @since 2021-08-17 00:30:45
 */
const log4js = require("log4js");

const levels = {
  trace: log4js.levels.TRACE,
  debug: log4js.levels.DEBUG,
  info: log4js.levels.INFO,
  warn: log4js.levels.WARN,
  error: log4js.levels.ERROR,
  fatal: log4js.levels.FATAL,
};

log4js.configure({
  appenders: {
    out: { type: "stdout" },
    app: { type: "file", filename: "logs/debug.log" },
    date: {
      type: "dateFile",
      filename: "logs/log",
      pattern: "yyyy-MM-dd.log",
      alwaysIncludePattern: true,
    },
  },
  categories: {
    // getLogger() 参数名称为此处
    default: {
      appenders: ["out"],
      level: "info",
    },
    debug: {
      appenders: ["app"],
      level: "debug",
    },
    error: {
      appenders: ["date"],
      level: "error",
    }
  },
});

/**
 * 日志输出,debug级别
 * @param {string}} content 需要输出的日志内容
 */
exports.debug = (content) => {
  let logger = log4js.getLogger('debug'); // 参数为空,默认找default
  logger.level = levels.debug;
  logger.debug(content);
};

/**
 * 日志输出, error 级别
 * @param {string}} content 需要输出的日志内容
 */
exports.error = (content) => {
  let logger = log4js.getLogger('error'); // 参数为空,默认找default
  logger.level = levels.error;
  logger.error(content);
};

/**
 * 日志输出, info 级别
 * @param {string}} content 需要输出的日志内容
 */
exports.info = (content) => {
  let logger = log4js.getLogger(); // 参数为空,默认找default
  logger.level = levels.info;
  logger.info(content);
};
